package com.cskaoyan.config;

import com.cskaoyan.converter.DateConverter;
import com.cskaoyan.interceptor.HandlerInterceptor1;
import com.cskaoyan.interceptor.HandlerInterceptor2;
import org.hibernate.validator.HibernateValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Primary;
import org.springframework.core.convert.support.ConfigurableConversionService;
import org.springframework.format.FormatterRegistry;
import org.springframework.validation.Validator;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.PostConstruct;

/**
 * 实现接口是为了做MVC的相关配置：mvc开头标签的配置 由该接口提供对应的方法来配置
 * @author stone
 * @date 2021/11/25 11:18
 */
@ComponentScan("com.cskaoyan.controller")
@EnableWebMvc
public class MvcConfiguration implements WebMvcConfigurer {
    //mvc:interceptors → HandlerInterceptor是谁、作用范围是什么、顺序
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //默认作用范围是“全局”
        registry.addInterceptor(new HandlerInterceptor1());
        registry.addInterceptor(new HandlerInterceptor2()).addPathPatterns("/hello/**");
    }

    //mvc:resources → mapping映射范围、location所处位置
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //mvc:resources                         mapping                          location
        registry.addResourceHandler("/pic1/**").addResourceLocations("classpath:/");
        registry.addResourceHandler("/pic2/**").addResourceLocations("/");
        registry.addResourceHandler("/pic3/**").addResourceLocations("file:d:/spring/");
    }
    /*//取出ConversionService
    @Autowired
    ConfigurableConversionService conversionService;
    //把Converter添加进去 → 生命周期的init方法
    @PostConstruct
    public void addConverter() {
        conversionService.addConverter(new DateConverter());
    }
    //放回去
    @Primary
    @Bean
    public ConfigurableConversionService conversionService() {
        return conversionService;
    }*/

    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverter(new DateConverter());
    }

    //这个组件id为固定值
    @Bean
    public CommonsMultipartResolver multipartResolver() {
        return new CommonsMultipartResolver();
    }

    @Override
    public Validator getValidator() {
        LocalValidatorFactoryBean localValidatorFactoryBean = new LocalValidatorFactoryBean();
        localValidatorFactoryBean.setProviderClass(HibernateValidator.class);
        return localValidatorFactoryBean;
    }
}
